/**
 * User domain class.
 */
class User extends DateTracker {
    static transients = ['pass', 'confirmPass', 'captcha']
    static hasMany = [roles:Role]
    static belongsTo = [Role]

    // User fields for security
    /** Username */
    String username
    /** MD5 Password */
    String password
    /** The person for this user */
    Person person
    /** enabled */
    boolean enabled = true

    /** plain password to create a MD5 password */
    String pass = '[secret]'

    String confirmPass
    String captcha

    static constraints = {
        username(nullable:true, unique:true)
        password(nullable:true)
        pass(nullable:true, blank:false, size:6..20, password:true,
                validator: {pass, obj ->
                    if ((pass == null && obj.confirmPass == null) || pass == obj.confirmPass) {
                        return true
                    }
                    return false
                })
        confirmPass(nullable:true, blank:false, size:6..20, password:true,
                validator: {confirmPass, obj ->
                    if ((confirmPass == null && obj.pass == null) || confirmPass == obj.pass) {
                        return true
                    }
                    return false
                })
        person(nullable:false, unique:true)
    }

    String toString() {
        return username
    }
}
